import 'package:ceo_app/utills/ceoFont.dart';
import 'package:ceo_app/api/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PublishPage extends StatefulWidget {
  const PublishPage({Key? key}) : super(key: key);

  @override
  PublishPageState createState() => PublishPageState();
}

class PublishPageState extends State<PublishPage> {
  List categoryList = [];

  void handleSelectCircle() async {
    debugPrint('你好');
    try {
      final res = await Api.getCateList();
      debugPrint('res==>${res.toString()}');
      setState(() {
        categoryList = res;
      });
    } catch (e) {
      debugPrint('错误了,res==>${e}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text('新发布', style: TextStyle(color: Colors.black)),
          backgroundColor: Colors.white,
          iconTheme: const IconThemeData(color: Colors.black),
          elevation: 0),
      body: Container(
        color: Colors.white,
        child: SingleChildScrollView(
            child: Column(
          children: [
            Container(
              height: 50,
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 14),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: const [
                        Icon(
                          CeoFont.planet,
                          color: Color(0xff222222),
                        ),
                        Text(
                          '未选择圈子',
                          style: TextStyle(
                              fontWeight: FontWeight.w600,
                              color: Color(0xFF333333),
                              fontSize: 16),
                        ),
                      ],
                    ),
                    GestureDetector(
                      onTap: handleSelectCircle,
                      child: Row(
                        children: const [
                          Text(
                            '选择圈子',
                            style: TextStyle(
                                color: Color(0xFF999999), fontSize: 14),
                          ),
                          Icon(
                            CeoFont.right,
                            size: 12,
                            color: Color(0xff222222),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
            Container(
              color: const Color(0XFFE7E7E7),
              height: 1,
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 6),
              child: Column(
                children: <Widget>[
                  Container(
                    // color: Colors.red,
                    // height: 120,
                    child: const TextField(
                      maxLines: 5,
                      maxLength: 2000,
                      // cursorHeight: 10,
                      cursorColor: Colors.black,
                      decoration: InputDecoration(
                          contentPadding: EdgeInsets.all(12),
                          hintStyle:
                              TextStyle(fontSize: 16, color: Color(0xFF999999)),
                          hintText: '说点什么',
                          border: InputBorder.none,
                          focusColor: Colors.black),
                    ),
                  ),
                  const SizedBox(height: 7),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 14),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    alignment: Alignment.center,
                    width: 169.w,
                    height: 36.w,
                    decoration: const BoxDecoration(
                      color: Color(0XFFF3F5F7),
                      borderRadius: BorderRadius.all(Radius.circular(4)),
                    ),
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: const [
                          Icon(
                            CeoFont.position,
                          ),
                          Text('位置', style: TextStyle(fontSize: 14)),
                        ]),
                  ),
                  Container(
                    width: 169.w,
                    height: 36.w,
                    decoration: const BoxDecoration(
                      color: Color(0XFFF3F5F7),
                      borderRadius: BorderRadius.all(Radius.circular(4)),
                    ),
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: const [
                          Icon(
                            CeoFont.peripheral,
                          ),
                          Text(
                            '周边',
                            style: TextStyle(fontSize: 14),
                          ),
                        ]),
                  ),
                ],
              ),
            ),
            // const SizedBox(height: 15),
            // const UploadImg(),
            const SizedBox(height: 51),
            Container(
              color: const Color(0XFFE7E7E7),
              height: 1,
            ),
            const SizedBox(
              height: 6,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: const <Widget>[
                // Spacer(),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 14),
                  child: ButtonTypesGroup(),
                )

                // Spacer(),
              ],
            ),
          ],
        )),
      ),
    );
  }
}

//发布按钮事件集合
class ButtonTypesGroup extends StatefulWidget {
  const ButtonTypesGroup({Key? key}) : super(key: key);

  @override
  State<ButtonTypesGroup> createState() => _ButtonTypesGroupState();
}

class _ButtonTypesGroupState extends State<ButtonTypesGroup> {
  void onPressed() async {
    try {
      final res = await Api.postPuhlish({
        "content": "入坑Switch的十大必玩游戏！丨游戏推荐",
        "images": [
          "https://pic3.zhimg.com/80/v2-e32d4d2e896b478a54356cab220d37ae_720w.webp",
          "https://pic4.zhimg.com/80/v2-246e5220db5e28af2b6f69c2c4d9cadb_720w.webp"
        ],
        "categoryIds": [1],
        "address": "上海-静安区"
      });
      debugPrint('res==>${res.toString()}');
    } catch (e) {
      debugPrint('错误了,res==>${e}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(4.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          SizedBox(
            width: 76.w,
            child: GestureDetector(
              onTap: onPressed,
              child: Container(
                alignment: Alignment.center,
                height: 36.w,
                width: 76.w,
                decoration: const BoxDecoration(
                    color: Color.fromARGB(255, 247, 176, 158),
                    borderRadius: BorderRadius.all(Radius.circular(20))),
                child: const Text(
                  '发布',
                  style: TextStyle(color: Color(0XFFFFFFFF), fontSize: 14),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

//上传图片组件
class UploadImg extends StatelessWidget {
  const UploadImg({super.key});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return const Text('图片');
  }
}
